package com.mc.book.utils.sse;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.sse.EventSource;
import okhttp3.sse.EventSources;

/**
 * TODO
 *
 * @Author: dong
 * @Date: 2024/7/2 14:28
 */
public class ExecuteSSEUtil {

    public static void executeSSE(String url, BookListener eventSourceListener, String chatGlm) throws Exception {
        RequestBody formBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), chatGlm);
        Request.Builder requestBuilder = new Request.Builder();
        Request request = requestBuilder.url(url).post(formBody).build();
        EventSource.Factory factory = EventSources.createFactory(SSEHttp.getInstance());
        //创建事件
        factory.newEventSource(request, eventSourceListener);
        eventSourceListener.getCountDownLatch().await();
    }
}
